﻿using qqdest.Model;
using qqdest.Service;
using qqdest.ServiceImpl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace qqdest.VC
{
    public partial class FrmRegister : Form
    {
        public FrmRegister()
        {
            InitializeComponent();
        }

        // 聚合 和 组合 UserService
        IUserService userService = null;
        
        /// <summary>
        /// 检测到注册页面所有输入框满足条件后, 使能注册按钮, 否则禁用注册按钮
        /// </summary>
        private void WatchText()
        {
            string nickname = textBox1.Text.Trim();
            string password = textBox2.Text.Trim();
            string phonenumber = textBox3.Text.Trim();

            button1.Enabled = (userService.CheckUserInput(nickname, 1) 
                               && userService.CheckUserInput(password, 6) 
                               && userService.CheckUserInput(phonenumber, 11));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();  // 触发 Close 事件, 让登录模态框能显示出来
        }

        private void button1_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.nickname    = textBox1.Text.Trim();
            user.password    = textBox2.Text.Trim();
            user.phonenumber = textBox3.Text.Trim();
            bool res = userService.Register(user);
            if (res)
            {
                MessageBox.Show("注册成功 : " + user.username, "注册提示");
                this.DialogResult = DialogResult.OK;  // 结束当前模态框.
            }
            else
            {
                MessageBox.Show("注册失败", "注册提示");
                textBox2.Clear();
            }
        }

        private void FrmRegister_Load(object sender, EventArgs e)
        {
            // 绑定 userService
            this.userService = new UserServuceImpl();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            WatchText();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            WatchText();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            WatchText();
        }
    }
}
